//
//  WmcWatermarkItemModel.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/3/29.
//

import Foundation
import SwCrypt
import UIKit

// MARK: - [WmcWatermarkOptionModel]

open class WmcWatermarkOptionModel: NSObject, YPNetworkJsonProtocol {
    // Lifecycle

    override public required init() {
        super.init()
    }

    // Open

    /// 标签，用于外部标记，不属于水印模型内部使用的字段
    open var label: String = ""

    /// 字段id
    open var fieldId: Int = 0

    /// 类型
    open var type: String = ""

    /// 字段名称
    open var title: String = ""

    /// 内容
    open var content: String = ""

    /// 是否可编辑标题 1：是 0：否
    open var enableEditTitle: Bool = false

    /// 内容是否可编辑
    open var enableEditContent: Bool = false

    /// 背景色
    open var bgColor: String = ""

    /// 文本颜色
    open var textColor: String = ""

    /// 是否设置显示该选项
    open var isOpen: Bool = false

    /// 是否可开关： 1可开关   0：不可开关
    open var isAction: Bool = false

    /// ⚠️用于兼容安卓的水印分享字段
    open var userOpen: Int?
    
    /// 选项，后端没有这个字段，他们把选项放到content里面很不合理，iOS新增一个字段解析选项
    open var options: [String] = []
    
    /// 品牌图
    open var brandAttribute: Brand?

    /// 创建一个字段
    /// - Parameters:
    ///   - label: 标签，用于外部标记，类似Key
    ///   - title: 标题
    ///   - content: 内容
    ///   - isOpen: 是否开启
    ///   - fieldType: 字段类型
    /// - Returns: WmcWatermarkOptionModel
    public static func Create(label: String,
                     title: String,
                     content: String,
                     isOpen: Bool = true,
                     fieldType: FieldType = .custom) -> WmcWatermarkOptionModel {
        let field = WmcWatermarkOptionModel()
        field.label = label
        field.title = title
        field.content = content
        field.isOpen = isOpen
        field.type = fieldType.rawValue
        return field
    }

    // Public

    public func mapping(mapper: HelpingMapper) {
        mapper <<< self.enableEditTitle <-- "editTitle"
        mapper <<< self.enableEditContent <-- "editContent"
        mapper <<< self.userOpen <-- "user_open"
    }

    // Internal

    /// 缓存哈希值
    func cacheHash() -> String {
        let mapList: [Any] = [
            type,
            enableEditTitle,
            enableEditContent,
            bgColor,
            textColor,
            isAction,
        ]
        let hash = mapList.yp.jsonStr?.yp.hashStr(.md5) ?? ""
        return hash
    }
}

extension WmcWatermarkOptionModel {
    /// 品牌图
    open class Brand: NSObject, YPNetworkJsonProtocol {
        // Lifecycle

        override public required init() {
            super.init()
        }

        // Open

        /// 本地图片，用于编辑
        open var localImage: WmcImage?

        /// 品牌图位置
        /// 0跟随水印 1左上角 2右上角 3中间 4左下角 5右下角
        open var location: Int = 0

        /// 品牌图模版id
        /// 0无
        open var templateId: Int = 0

        /// 透明度 0-100
        open var alpha: Int = 0

        /// 默认大小  0-100
        open var size: Int = 20

        /// 圆角
        open var cornerRadius: CGFloat = 8

        // Public

        public func mapping(mapper: HelpingMapper) {
            mapper <<< self.templateId <-- "style"
            mapper <<< self.location <-- "location"
            mapper <<< self.alpha <-- "opacity"
        }

        // Internal

        enum LocationType: Int {
            case followWatermark = 0
            case topLeft
            case topRight
            case center
            case bottomLeft
            case bottomRight

            struct Info {
                var title: String
            }

            var info: Info {
                switch self {
                case .followWatermark: return .init(title: "跟随水印")
                case .topLeft: return .init(title: "左上角")
                case .topRight: return .init(title: "右上角")
                case .center: return .init(title: "中间")
                case .bottomLeft: return .init(title: "左下角")
                case .bottomRight: return .init(title: "右下角")
                }
            }
        }

        /// 位置类型
        var locationType: LocationType {
            return .init(rawValue: location) ?? .followWatermark
        }
    }

    /// 品牌图读取
    open var brand: Brand {
        if let brandAttribute = brandAttribute {
            return brandAttribute
        } else {
            return .init()
        }
    }
}

public extension WmcWatermarkOptionModel {
    enum FieldType: String {
        case unknow
        /// 标题
        case title
        /// 副标题
        case sub_title
        /// 时间
        case time
        /// 地点
        case addr
        /// 天气
        case weather
        /// 经纬度
        case lat
        /// 备注
        case remark
        /// 记工
        case record
        /// 输入
        case text
        /// 隔离记录第N天特殊字段
        case isolation
        /// 品牌Logo
        case brand
        /// 海拔
        case altitude
        /// 方位角
        case azimuth
        /// 分贝
        case decibe
        /// 速度
        case speed
        /// 长度
        case length
        /// 单选
        case single
        /// 数钢筋数量
        case results

        /// 自定义
        case custom
    }
}

extension WmcWatermarkOptionModel.FieldType {
    struct Info { // 我当然知道直接String也行， 但是预留，方便后期扩展
    }

    var info: Info {
        switch self {
        case .unknow: return .init()
        case .title: return .init()
        case .sub_title: return .init()
        case .time: return .init()
        case .addr: return .init()
        case .weather: return .init()
        case .lat: return .init()
        case .remark: return .init()
        case .record: return .init()
        case .text: return .init()
        case .isolation: return .init()
        case .brand: return .init()
        case .altitude: return .init()
        case .azimuth: return .init()
        case .decibe: return .init()
        case .speed: return .init()
        case .length: return .init()
        case .single: return .init()
        case .results: return .init()
        case .custom: return .init()
        }
    }
}

public extension WmcWatermarkOptionModel {
    /// 字段类型
    var fieldType: WmcWatermarkOptionModel.FieldType {
        return .init(rawValue: type) ?? .unknow
    }
}

extension WmcWatermarkOptionModel {
    var uiBgColor: UIColor {
        return uiBgColor()
    }

    func uiBgColor(_ def: UIColor = .clear) -> UIColor {
        if bgColor.isEmpty {
            return def
        }
        return bgColor.yp.uiColor
    }

    var uiTextColor: UIColor {
        return uiTextColor()
    }

    func uiTextColor(_ def: UIColor = .white) -> UIColor {
        if textColor.isEmpty {
            return def
        }
        return textColor.yp.uiColor
    }

    static var defaultContentText = "点击编辑"
    static var defaultRecordContentText = "请输入工时，例：1个工"
    static var defaultIsolationContentText = "请输入天数，如：1"

    var contentText: String {
        return contentText()
    }

    func contentText(_ def: String? = nil) -> String {
        if content.isEmpty {
            switch fieldType {
            case .record:
                return WmcWatermarkOptionModel.defaultRecordContentText
            case .isolation:
                return WmcWatermarkOptionModel.defaultIsolationContentText
            default:
                return WmcWatermarkOptionModel.defaultContentText
            }
        }
        return content
    }

    func drawContentText() -> String {
        if content.isEmpty {
            return " "
        }
        return content
    }
}
